package com.icat.house.web.controller.house;

import com.alibaba.fastjson.JSONObject;
import com.icat.house.common.Constant;
import com.icat.house.model.HouseInfo;
import com.icat.house.model.UserInfo;
import com.icat.house.model.UserTradeRoom;
import com.icat.house.service.HouseService;
import com.icat.house.web.controller.BaseController;
import com.icat.house.web.intercept.LoginIntercept;
import com.jfinal.aop.Before;
import com.jfinal.aop.Clear;
import com.jfinal.core.ActionKey;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.kit.StrKit;

@Before(LoginIntercept.class)
@ControllerBind(controllerKey = "/list")
public class HouseController extends BaseController {
	/**
	 * 房源信息
	 */
	@Clear
	@ActionKey("/list/buildings")
	public void house_buildings() {
		setAttr("buildings", HouseInfo.dao.findBuildings());
		render("/page/houseList.html");
	}

	/**
	 * 列表
	 */
	@ActionKey("/list/house")
	public void house_list() {
		int buildings = getParaToInt("buildings", 1);
		renderJson(HouseService.findHouse(buildings));
	}

	/**
	 * 购买
	 */
	@ActionKey("list/trade")
	public void houseTrade() {
		UserInfo info = getAttr("user");
		int status = getParaToInt("status");
		String id = getPara("id");
		HouseInfo houseInfo = HouseInfo.dao.updateStauts(id);
		JSONObject object = new JSONObject();
		if (StrKit.notNull(houseInfo)) {
			int mark = houseInfo.getInt("status");
			if (status == 2) {
				if (mark == 0) {
					if (UserTradeRoom.dao.save(info.getId(), id, status) && houseInfo.set("status", status).update()) {
						object.put(Constant.SUCCESS, Constant.SUCCESS_MESSAGE_SAVE);
					} else {
						object.put(Constant.ERROR, Constant.ERROR_MESSAGE_SAVE);
					}
				} else {
					object.put(Constant.ERROR, Constant.ERROR_MESSAGE_RESERVE);
				}
			} else if (status == 1) {
				if (mark == 2) {
					UserTradeRoom tradeRoom = UserTradeRoom.dao.getUserTradeRoom(info.getId(), id, 2);
					if (StrKit.notNull(tradeRoom)) {
						houseInfo.set("status", status).update();
						if (UserTradeRoom.dao.save(info.getId(), id, status)) {
							tradeRoom.delete();
							object.put(Constant.SUCCESS, Constant.SUCCESS_MESSAGE_SAVE);
						} else {
							object.put(Constant.ERROR, Constant.ERROR_MESSAGE_SAVE);
						}
					} else {
						object.put(Constant.ERROR, Constant.ERROR_MESSAGE_NO_RESERVE);
					}
				} else {
					object.put(Constant.ERROR, Constant.ERROR_MESSAGE_RESERVE);
				}
			} else if (status == 0) {
				if (mark == 2) {
					houseInfo.set("status", status).update();
					if (UserTradeRoom.dao.deleteHouseTrade(info.getId(), id)) {
						object.put(Constant.SUCCESS, Constant.SUCCESS_MESSAGE_NO_RESERVE);
					} else {
						object.put(Constant.SUCCESS, Constant.SERVICE_ERROR);
					}
				} else {
					object.put(Constant.ERROR, Constant.ERROR_MESSAGE_SAVE);
				}
			}
		} else {
			object.put(Constant.ERROR, Constant.ERROR_MESSAGE_SAVE);
		}

		renderJson(object);
	}

	@Before(LoginIntercept.class)
	@ActionKey("/list/delete/trade")
	public void deleteTradeHouse() {
		UserInfo info = getAttr("user");
		HouseInfo.dao.updateHouse(info.getId(), 2);
		renderNull();
	}
}
